package com.xjh.utils.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author XuJianHua
 * @date 2022-08-29 16:40:45
 * @description
 */
public class DateUtils {
    static String timeStamp2Date(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间戳
        //long l = System.currentTimeMillis();
        //如果你数据库存储的时间戳类型为string，就需要将string字符串转为long类型

        long l = Long.parseLong(dateString);
        String format = sdf.format(l);
        System.out.println("日期格式：" + format);
        //输出：日期格式：2020-10-11 10:42:01
        return format;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds   精确到秒的字符串
     * @param formatStr
     * @return
     */

    public static String timeStamp2Date(String seconds, String format) {

        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {

            return "";

        }

        if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(new Date(Long.valueOf(seconds + "000")));

    }


    public static void main(String[] args) {

        System.out.println(timeStamp2Date("1661742728"));

        System.out.println("jjj"+timeStamp2Date("1661742728", "yyyy-MM-dd"));
        System.out.println("jjj"+timeStamp2Date("1661742687", "yyyy-MM-dd HH:mm:ss"));
    }
}
